package hoang.android.app.adapter;

import hoang.android.app.R;
import hoang.android.app.item.ItemVideo;
import hoang.android.app.json.ParserImage;
import hoang.android.app.model.Video;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

public class VideoAdapter extends ArrayAdapter<Video>{
	private Context mContext;
	private ArrayList<Video> arrayVideos;
	
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	public VideoAdapter(Context context, int textViewResourceId,ArrayList<Video> objects) {
		super(context, textViewResourceId, objects);
		
		this.mContext = context;
		this.arrayVideos = objects;
		
		imageLoader = ImageLoader.getInstance();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				mContext).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				// .showImageOnFail(R.drawable.img_demo).cacheInMemory()
				.cacheOnDisc().resetViewBeforeLoading() // default
				.imageScaleType(ImageScaleType.EXACTLY) // default
				.bitmapConfig(Bitmap.Config.ARGB_8888) // default
				.displayer(new SimpleBitmapDisplayer()) // default
				.handler(new Handler()) // default
				.build();
		imageLoader.init(config);
	}
	
	@SuppressWarnings("static-access")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View videoView = convertView;
		if(videoView ==null){
			videoView = new ItemVideo(mContext);
		}
		final Video video = arrayVideos.get(position);
		if(video!=null){
			ImageView imgThumbnail = ((ItemVideo)videoView).imgThumbnail;
			TextView tvTitle = ((ItemVideo)videoView).tvTitle;
			TextView tvDuration = ((ItemVideo)videoView).tvDuration;
			TextView tvNumberViews = ((ItemVideo)videoView).tvNumberViews;
			CheckBox cbChecked = ((ItemVideo)videoView).cbChecked;
			cbChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					video.setChecked(isChecked);
				}
			});
			
//			imgThumbnail.setImageBitmap(ParserImage.getBitmap(video.getUrlThumbnail()));
			
			imageLoader.getInstance().displayImage(video.getUrlThumbnail(), imgThumbnail, options);
			
			tvTitle.setText(video.getTitleVideo());
			tvDuration.setText(video.getDurationVideo());
			tvNumberViews.setText(video.getNumberViewsVideo());
			cbChecked.setChecked(video.isChecked());
		}
		return videoView;
	}
	
}
